(function(){
    function showError(error){
        switch(error.code) {
            case error.PERMISSION_DENIED:
                alert("定位失败,用户拒绝请求地理定位");
                break;
            case error.POSITION_UNAVAILABLE:
                alert("定位失败,位置信息是不可用");
                break;
            case error.TIMEOUT:
                alert("定位失败,请求获取用户位置超时");
                break;
            case error.UNKNOWN_ERROR:
                alert("定位失败,定位系统失效");
                break;
        }
    }
    function getLocation(){
        if (navigator.geolocation){
            //console.log(1);
            navigator.geolocation.getCurrentPosition(showPosition,showError);
        }else{
            alert("浏览器不支持地理定位。");
        }
    }
    getLocation();
    function showPosition(position){
        var latlon = position.coords.latitude+','+position.coords.longitude;
        //baidu
        var url = "http://api.map.baidu.com/geocoder/v2/?ak=CxOF3vPpGuVrdKjirpGmw8VsAf9BcrnP&callback=renderReverse&location="+latlon+"&output=json&pois=0";
        //var url = 'http://maps.google.cn/maps/api/geocode/json?latlng='+latlon+'&language=CN';
        $.ajax({
            type: "GET",
            dataType: "jsonp",
            url: url,
            beforeSend: function(){
                //$(".iconfont").transform('正在定位...');
            },
            success: function (json) {
                //console.log(1);
                if(json.status==0){
                    var str=json.result.formatted_address;
                    var index=str.indexOf('市');
                    $(".s_location").html(str.substring(index+1));
                }
//            if(json.status=='OK'){
//                var results = json.results;
//                $.each(results,function(index,array){
//                    if(index==0){
//                        $(".s_location").html(array['formatted_address']);
//                    }
//                });
//            }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $(".s_location").html(latlon+"地址位置获取失败");
            }
        });
    };
    //console.log(1);
    $('.my_location').on('click',function(){
        //console.log(1);
        $('.groupNearby').hide();
        $('.locationSearch').show();
    });
    $('.b_search').on('click',function(){
        var keyword=$('.i_position').val();
        var url='http://api.map.baidu.com/place/v2/search?q='+keyword+'&region=北京&output=json&ak={CxOF3vPpGuVrdKjirpGmw8VsAf9BcrnP}';

        $.ajax({
            url:url,
            dataType:'jsonp',
            type:'GET',
            success:function(json){
                alert(json);
            }
        })
    });
})();
